local Config = require("Config")
local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local AbsActDefine = require("absAct.AbsActDefine")
local ObjHuman = require("core.ObjHuman")
local MoshouLogic = require("moshou.MoshouLogic")
local AbsActLogic = require("absAct.AbsActLogic")



local function initDB(human, funcID)
    local actDB = human.db.absAct[funcID]
    if not actDB then return end

    local config = AbsActExcel.absMoShouXiLian
    if not config then return end

    for k, v in ipairs(config) do
        actDB[v.buyid] = actDB[v.buyid] or {}
        actDB[v.buyid].cnt = actDB[v.buyid].cnt or v.number
        actDB[v.buyid].max = actDB[v.buyid].max or v.number     
    end
end


--
function isOpen(human, YYInfo, funcConfig)
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end

    local xilianOpen = MoshouLogic.xilianIsOpen(human)
    if xilianOpen == 1 then
        return true, endTime, startTime
    end
end

--
function isRed(human, YYInfo, funcConfig)
    local config = AbsActExcel.absMoShouXiLian
    if not config then return end

    local actDB = human.db.absAct[funcConfig.funcID]
    if not actDB then return end
    for k, v in ipairs(config) do
       if actDB[v.buyid] and actDB[v.buyid].cnt and actDB[v.buyid].cnt > 0 then
          return true
       end
    end
end

--
function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

-- 
function onBuyLongXue(human, funcID, buyId)
    local absAct = human.db.absAct[funcID]
    if not absAct then return end

    -- 活动过期  没有双倍
    local state, endTime, startTime = AbsActLogic.isStarted(human, funcID)
    if not state then return end

    AbsActLogic.checkAbsActClean(human, funcID)
    initDB(human, funcID)

    if not absAct[buyId] then return end

    if absAct[buyId].cnt <= 0 then

       return
    end

    absAct[buyId].cnt = absAct[buyId].cnt - 1

    return true
end

--
function onQueryLongXue(human, funcID, parameter)
    AbsActLogic.checkAbsActClean(human, funcID)
    initDB(human, funcID)

    return human.db.absAct[funcID]
end

-- 
function updateDaily(human, funcID)
    local state, endTime, starTime = AbsActLogic.isStarted(human, funcID)
    if not state then return end

    local config = AbsActExcel.absActivity[funcID]
    if not config then return end

    local config = AbsActExcel.absMoShouXiLian
    if not config then return end

    AbsActLogic.checkAbsActClean(human, funcID)

    local actDB = human.db.absAct[funcID]
    for k, v in ipairs(config) do
       actDB[v.buyid] = nil
    end
end